package pl.bank.model;

import java.util.Date;

import oracle.adf.share.logging.ADFLogger;

import oracle.jbo.Key;
import oracle.jbo.Row;
import oracle.jbo.RowIterator;
import oracle.jbo.ViewCriteria;
import oracle.jbo.domain.Number;
import oracle.jbo.domain.Timestamp;
import oracle.jbo.server.ApplicationModuleImpl;

import pl.bank.model.common.AdminAppModule;
import pl.bank.model.view.AccountsListImpl;
import pl.bank.model.view.AccountsListRowImpl;
import pl.bank.model.view.AccountsViewRowImpl;
import pl.bank.model.view.TransactionListImpl;
import pl.bank.model.view.TransactionListRowImpl;
import pl.bank.model.view.TransactionsViewRowImpl;


// ---------------------------------------------------------------------
// ---    File generated by Oracle ADF Business Components Design Time.
// ---    Fri May 25 20:40:10 CEST 2012
// ---    Custom code may be added to this class.
// ---    Warning: Do not modify method signatures of generated methods.
// ---------------------------------------------------------------------
public class AdminAppModuleImpl extends ApplicationModuleImpl implements AdminAppModule {

    private static final ADFLogger LOG = ADFLogger.createADFLogger(AdminAppModuleImpl.class);

    /**
     * This is the default constructor (do not remove).
     */
    public AdminAppModuleImpl() {
    }

    public void makeTransactions() {

        RootAppModImpl root = (RootAppModImpl)getRootApplicationModule();
        BankAppModuleImpl bank = (BankAppModuleImpl)root.getBankAppModuleInstance();

        AccountsListImpl aView = getAccountsListView();
        TransactionListImpl tran = getTransactionListView();
        ViewCriteria criteria = tran.getViewCriteria("TransactionListCriteria");
        ViewCriteria aCriteria = aView.getViewCriteria("AccountsListCriteria");
        tran.applyViewCriteria(criteria);
        tran.executeQuery();
        RowIterator tView = getTransactionListView();
        LOG.fine("tView " + tView.getRowCount());
        Timestamp currentDate = new Timestamp(new Date());

        while (tView.hasNext()) {
            TransactionListRowImpl tRow = (TransactionListRowImpl)tView.next();

            aView.setAccountNumber(tRow.getTrAcNumerReciver());
            aView.applyViewCriteria(aCriteria);
            aView.executeQuery();
            Row[] aRows = aView.getAllRowsInRange();
            LOG.info("rows " + aRows.length);
            if (aRows.length > 0) {
                AccountsListRowImpl aRow = (AccountsListRowImpl)aRows[0];

                tRow.setTrRealizationDate(currentDate);
                Number ZERO = new Number(0);
                Number tAmount = ZERO.subtract(tRow.getTrAmount());
                LOG.fine("tAmount " + tAmount);
                Number aAmount = aRow.getAcAvailableAmount().add(tAmount);
                aRow.setAcAvailableAmount(aAmount);

                Object[] keyObj = new Object[1];
                keyObj[0] = aRow.getAcAccountId();
                Key key = new Key(keyObj);
                Row[] rows = bank.getAccountsViewDef().findByKey(key, 1);
                AccountsViewRowImpl acRow = (AccountsViewRowImpl)rows[0];

                TransactionsViewRowImpl newTRow = (TransactionsViewRowImpl)acRow.getTransactionsView().createRow();
                newTRow.setTrAccountId(aRow.getAcAccountId().getSequenceNumber());
                newTRow.setTrType(new Number(1));
                newTRow.setTrAmount(tAmount);
                newTRow.setTrCreationDate(currentDate);
                newTRow.setTrRealizationDate(currentDate);
                acRow.getTransactionsView().insertRow(newTRow);

            }
        }


        aView.setAccountNumber(null);
        aView.applyViewCriteria(aCriteria);
        aView.executeQuery();
        RowIterator aIter = aView;

        LOG.fine("aIter " + aIter.getRowCount());
        while (aIter.hasNext()) {
            AccountsListRowImpl aRow = (AccountsListRowImpl)aIter.next();
            aRow.setAcAmount(aRow.getAcAvailableAmount());
        }

    }

    /**
     * Container's getter for AccountsListView.
     * @return AccountsListView
     */
    public AccountsListImpl getAccountsListView() {
        return (AccountsListImpl)findViewObject("AccountsListView");
    }

    /**
     * Container's getter for TransactionListView.
     * @return TransactionListView
     */
    public TransactionListImpl getTransactionListView() {
        return (TransactionListImpl)findViewObject("TransactionListView");
    }
}
